package gat.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonUtil {
    private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);

    public JsonUtil() {
    }

    public static ObjectMapper getObjectMapper() {
        return (new Jackson2ObjectMapperBuilder()).createXmlMapper(false).build().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true).setSerializationInclusion(Include.NON_NULL);
    }

    public static String toString(Object obj) {
        try {
            return getObjectMapper().writeValueAsString(obj);
        } catch (Throwable var2) {
            log.error("JSON.toJSONString 异常，默认返回空字符串：{}", var2.getMessage(), var2);
            return "";
        }
    }

    public static String toJsonString(Object obj) {
        return toString(obj);
    }

    public static ObjectNode createJsonObjectNode() {
        return getObjectMapper().createObjectNode();
    }

    public static JsonNode readJsonObjectNode(String jsonData) {
        try {
            return getObjectMapper().readTree(jsonData);
        } catch (Exception var2) {
            log.error("readJsonObjectNode-->字符串{}转换JsonNode异常，默认返回对象", jsonData, var2);
            return null;
        }
    }

    public static <T> T jsonToPojo(JsonNode jsonData, Class<T> beanType) {
        try {
            return getObjectMapper().treeToValue(jsonData, beanType);
        } catch (Exception var3) {
            log.error("jsonToPojo-->JsonNode{}转换{}异常，默认返回对象", new Object[]{jsonData, beanType.getTypeName(), var3});
            return null;
        }
    }

    public static ArrayNode createJsonArrayNode() {
        return getObjectMapper().createArrayNode();
    }

    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            return getObjectMapper().readValue(jsonData, beanType);
        } catch (Exception var3) {
            log.error("jsonToPojo-->字符串{}转换{}异常，默认返回对象", new Object[]{jsonData, beanType.getTypeName(), var3});
            return null;
        }
    }

    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        ObjectMapper objectMapper = getObjectMapper();
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, new Class[]{beanType});

        try {
            return (List)objectMapper.readValue(jsonData, javaType);
        } catch (Exception var5) {
            log.error("jsonToList-->字符串{}转换{}异常，默认返回对象", new Object[]{jsonData, beanType.getTypeName(), var5});
            return new ArrayList();
        }
    }

    public static <T> T jsonToPojo(String jsonData, JavaType javaType) {
        try {
            return getObjectMapper().readValue(jsonData, javaType);
        } catch (Exception var3) {
            log.error("jsonToPojo-->字符串{}转换{}异常，默认返回对象", new Object[]{jsonData, javaType.getTypeName(), var3});
            return null;
        }
    }

    public static Map<String, Object> beanToMap(Object obj) throws Exception {
        return (Map)getObjectMapper().readValue(toString(obj), new TypeReference<Map<String, Object>>() {
        });
    }
}
